package action.login;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import security.MySecurityContextHandle;
import service.interf.security.IGroup;
import service.interf.security.IGroupMember;
import service.interf.security.IMenu;

import com.opensymphony.xwork2.Action;

import dao.pojo.Group;
import dao.pojo.Menu;

public class AdminAction implements Action {
	private String url;
	private IGroup groupImpl;
	private String userName;
	private IMenu menuImp;
	private IGroupMember groupMemberImpl;

	public IGroupMember getGroupMemberImpl() {
		return groupMemberImpl;
	}

	public void setGroupMemberImpl(IGroupMember groupMemberImpl) {
		this.groupMemberImpl = groupMemberImpl;
	}

	public IMenu getMenuImp() {
		return menuImp;
	}

	public void setMenuImp(IMenu menuImp) {
		this.menuImp = menuImp;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setGroupImpl(IGroup groupImpl) {
		this.groupImpl = groupImpl;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String execute() throws Exception {
		// 清除session
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.removeAttribute("userName");
		session.removeAttribute("userId");
		session.removeAttribute("uuid");
		session.removeAttribute("loginUrl");
		session.removeAttribute("menuList");
		// 当前登录用户的组信息和菜单信息
		List<Group> gL = groupMemberImpl
				.getGroupByUserId(MySecurityContextHandle.getUserId());
		for (Group g : gL) {
			String loginUrl = g.getLoginUrl();
			if (!StringUtils.isEmpty(loginUrl)) {
				url = loginUrl;
				break;
			}
		}
		Map<String, List<Menu>> menuMap = menuImp.getGroupMenusByUserId(gL);
		if (StringUtils.isEmpty(url)) {
			url = "/pages/web/index.jsp";
		}
		session.setAttribute("userName", MySecurityContextHandle.getUserName());
		session.setAttribute("uuid", MySecurityContextHandle.getUUId());
		session.setAttribute("userId", MySecurityContextHandle.getUserId()
				.toString());
		session.setAttribute("loginUrl", url);
		session.setAttribute("menuList", menuMap);
		return "success";
	}
}
